<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>用户登录</title>
  <script src="js/vue.min.js"></script>
  <script src="js/jquery-1.12.4.js"></script>
</head>

<body>
  <div id="app">
    <form action="#">
      帐号：<input type="text" name="uname" v-model="uname"/><br />
      密码：<input type="text" name="upass" v-model="upass"/><br />

      <div v-if="isLoginErr" style="color:red">错误的帐号或密码</div>
      
      <button type="button" @click="login">登录</button>
    </form>
  </div>

  <script>
    new Vue({
      el:'#app' ,

      data:{
        uname:'zing',
        upass:'123456',
        isLoginErr : false 
      } ,

      methods:{
        login() {

          // 初始化数据 -- 准备向服务器传递的数据
          let params = {
            username : this.uname ,
            password : this.upass 
          }

          let that = this ;

          $.ajax({
            url:'http://localhost:9999/tiangou/api/login',
            data:params,
            dataType:'json',
            type:'POST',
            success:function( res ) {
              // console.log( res );

              if (res.code == 200) {
                // 把 token 令牌字符串，保存在本地浏览器中
                // 便于下次请求时，读取token，并发送回给服务器
                localStorage.setItem("token", res.data);
                
                // 跳转页面 
                location.href = "main.html"
                return;
              }

              that.isLoginErr = true;
            }
          }) ;
        }
      }


    }) ;
  </script>


</body>

</html>